"""
时间敏感度分析器
根据交易时间和股票流动性确定信号敏感度等级
"""
import logging
from datetime import datetime

logger = logging.getLogger('SensitivityAnalyzer')


class TimeSensitivityAnalyzer:
    """
    时间敏感度分析器
    根据交易时间和股票流动性动态调整信号敏感度
    """
    
    def analyze(self, current_time: datetime, liquidity: str) -> str:
        """
        分析时间敏感度
        
        Args:
            current_time: 当前时间
            liquidity: 流动性等级 'high' | 'medium' | 'low'
            
        Returns:
            str: 敏感度等级 'high' | 'medium' | 'low' | 'normal'
        """
        # 计算开盘后的分钟数
        minute_of_day = current_time.hour * 60 + current_time.minute
        opening_minute = 9 * 60 + 30  # 9:30
        time_diff = minute_of_day - opening_minute
        
        # 基础时间分级
        if time_diff < 0:  # 开盘前
            return 'high'
        elif time_diff <= 15:  # 9:30-9:45（开盘15分钟）
            base_sensitivity = 'high'
        elif time_diff <= 30:  # 9:45-10:00（开盘30分钟）
            base_sensitivity = 'medium'
        elif time_diff <= 60:  # 10:00-10:30（开盘1小时）
            base_sensitivity = 'low'
        else:  # 10:30以后
            base_sensitivity = 'normal'
        
        # 根据流动性调整敏感度
        adjusted_sensitivity = self._adjust_by_liquidity(base_sensitivity, liquidity)
        
        return adjusted_sensitivity
    
    def _adjust_by_liquidity(self, base_sensitivity: str, liquidity: str) -> str:
        """
        根据流动性调整敏感度
        
        Args:
            base_sensitivity: 基础敏感度
            liquidity: 流动性等级
            
        Returns:
            str: 调整后的敏感度
        """
        # 低流动性股票在开盘时段更敏感（量比更容易失真）
        if liquidity == 'low' and base_sensitivity in ['high', 'medium']:
            return 'high'
        
        # 高流动性股票在开盘时段相对不那么敏感
        elif liquidity == 'high' and base_sensitivity == 'high':
            return 'medium'
        
        return base_sensitivity

